package com.qfedu.yanglao20admin.adhibition.service.impl;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.qfedu.yanglao20.common.constants.CommonConstants;
import com.qfedu.yanglao20.common.enuminfo.StateEnum;
import com.qfedu.yanglao20.common.exeception.CommonException;
import com.qfedu.yanglao20.entity.adhibition.Adhibition;
import com.qfedu.yanglao20admin.adhibition.dao.AdhibitionDao;
import com.qfedu.yanglao20admin.adhibition.param.AdhibitionParam;
import com.qfedu.yanglao20admin.adhibition.service.AdhibitionService;
import com.qfedu.yanglao20admin.adhibition.vo.AdhibitionVO;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.List;

/**
 * @author yujie
 * @data 2023/4/19 21:11
 */
@Service
public class AdhibitionServiceImpl implements AdhibitionService {

    @Resource
    private AdhibitionDao adhibitionDao;

    @Override
    public Page<AdhibitionVO> pageAdhibition(AdhibitionParam param) {

        Integer offset = (param.getPageNum() - 1) * param.getPageSize();
        param.setOffset(offset);

        // 分页查询
        Page<AdhibitionVO> page = new Page<>(param.getPageNum(), param.getPageSize());
        List<AdhibitionVO> adhibitions = adhibitionDao.selectList(param);

        //  循环遍历
        for (AdhibitionVO vo :
                adhibitions) {
            StateEnum stateById = StateEnum.getStateById(vo.getEnableFlag());
            vo.setStateDesc(stateById.getDesc());
        }

        // 页码
        page.setCurrent(param.getPageNum());
        page.setSize(param.getPageSize());

        // 总记录数
        Long aLong = adhibitionDao.selectCount(null);
        page.setTotal(aLong);

        page.setRecords(adhibitions);

        return page;
    }


    @Override
    public void deleteById(Integer id) {
        adhibitionDao.deleteById(id);
    }


    @Override
    public void addAdhibition(Adhibition adhibition) {

        if (adhibition.getApplyName() == null || adhibition.getApplyName().isEmpty()) {
            throw new CommonException(CommonConstants.ERROR, "请填服务名称");

        }

        Adhibition byApplyName = adhibitionDao.selectByApplyName(adhibition.getApplyName());
        if (byApplyName != null) {
            throw new CommonException(CommonConstants.ERROR, "该服务名称已存在");
        }

        adhibitionDao.insert(adhibition);
    }

    @Override
    public Adhibition queryAdhibition(Integer id) {
        return adhibitionDao.selectById(id);
    }

    @Override
    public void updateAdhibition(Adhibition adhibition) {

        if (adhibition.getApplyName() == null || adhibition.getApplyName().isEmpty()) {
            throw new CommonException(CommonConstants.ERROR, "请填服务名称");
        }

        Adhibition byApplyName = adhibitionDao.selectByApplyName(adhibition.getApplyName());
        if (byApplyName != null) {
            if (!byApplyName.getId().equals(adhibition.getId())) {
                throw new CommonException(CommonConstants.ERROR, "该服务名称已存在");
            }

        }

        adhibitionDao.updateById(adhibition);
    }
}
